<?php

namespace app\home\controller;

use app\common\controller\QyApi;
use app\company\service\DataService;
use library\Controller;
use think\Db;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;

use function Qiniu\json_decode;

/**
 * 打卡记录分析
 * Class User
 * @package app\worker\controller
 */
class CardInfo extends Controller
{
    /**
     * 打卡记录分析
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '打卡记录分析';
        $this->list = [];
        $this->fetch();
    }

    public function import(){
        set_time_limit(60); //限制页面执行时间60s
        $file = $_FILES['file']['tmp_name']; //临时文件路径或者文件url
        try {
            $file = iconv("utf-8", "gb2312", $file); //转码
            if (empty($file) or !file_exists($file)) {
                throw new \Exception('文件不存在!');
            }
            $objReader = IOFactory::createReader('Xlsx');
            if (!$objReader->canRead($file)) {
                $objReader = IOFactory::createReader('Xls');
                if (!$objReader->canRead($file)) {
                    throw new \Exception('只支持导入Excel文件！');
                }
            }
            $objReader->setReadDataOnly(true); //忽略任何格式的信息
            $objPHPExcel = $objReader->load($file);
            $countSheet = $objPHPExcel -> getSheetCount();    //获取工作表数量
            $data = [];
            for ($i = 2; $i < $countSheet; $i++)
            {
                $currentSheet = $objPHPExcel -> getSheet($i);   //获取工作表内容
                $dataArray = $currentSheet -> toArray();       //转换为数组
                //表头
                $header = $dataArray[0];
                //内容
                for($j = 1; $j < count($dataArray); $j++){
                    $info[] = $dataArray[$j];
                }
            }
            $data = [
                'header' => $header,
                'info' => $info,
            ];
            return json(['code' => 1, 'data' => $data ,'msg' => '成功']);
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }
}
